﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyBoss2Core : EnemyBaseFixed
{
    float leftLimit=5.3f;
    float rightLimit=10.625f;
    private SeqAction workAction=new(){};
    private Sprite[] bossSprites;
    private CameraLevel2 cameraLeve2;
    private void Awake()
    {
        base.Init();
        bossSprites=new Sprite[]{
            ResourcesPool.Sprites["魂-第二关-Boss-闪1"],
            ResourcesPool.Sprites["魂-第二关-Boss-闪2"],
            ResourcesPool.Sprites["魂-第二关-Boss-闪3"],
            ResourcesPool.Sprites["魂-第二关-Boss-闪4"],
            ResourcesPool.Sprites["魂-第二关-Boss-闪5"],
            ResourcesPool.Sprites["魂-第二关-Boss-闪6"],
            ResourcesPool.Sprites["魂-第二关-Boss-闪7"],
            ResourcesPool.Sprites["魂-第二关-Boss-闪8"],
            ResourcesPool.Sprites["魂-第二关-Boss-闪9"],
            ResourcesPool.Sprites["魂-第二关-Boss-闪10"],
        };

        fireAction=new(){intervalTime=1f};
        cameraLeve2=Camera.main.GetComponent<CameraLevel2>();
    }

    void Start()
    {
        //Run();
        // Stop();

    }
    private void FixedUpdate()
    {
        base.CommonFixedUpdate();
    }
    protected void UpdateStateNone(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState=EnumState.Working;
    }
    protected void UpdateStateWorking(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState=inState;
        rig.velocity=lookAt.x*5f*Vector2.right;
        workAction.Update((SeqAction seq)=>{
            int index=Mathf.FloorToInt(seq.DurationTime/(4f/60f))%bossSprites.Length;
            mainSR.sprite=bossSprites[index];

            if(transform.position.x<=leftLimit){
                lookAt.x=1;
            }else if(transform.position.x>=rightLimit){
                lookAt.x=-1;
            }


        });
        fireAction.Update((SeqAction seq)=>{
            Fire();
        });
    }

    protected override void UpdateStateDie(bool stateChanged, EnumState inState, out EnumState outState)
    {
        base.UpdateStateDie(stateChanged, inState, out outState);
        if (stateChanged)
        {
            Instantiate(Constant.Prefabs.EnemyBoom1.Load(),transform.position,Quaternion.identity);
            Destroy(gameObject);
        }
    }

    private void Fire(){
        Instantiate(Constant.Prefabs.EnemyBoss2Bullet.Load(),transform.position,Quaternion.identity);
    }


}
